<?php

namespace app\admin\service\dict;

use app\common\model\dict\DictData;
use tmcore\base\BaseService;

/**
 * 字典数据服务类
 */
class DataService extends BaseService
{
    /**
     * 获取字典数据列表
     * @return array
     */
    public static function getList(): array
    {
        // 搜索条件
        $searchParam = [
            '=' => ['status', 'type_id'],
            '%like%' => ['name']
        ];
        $where = self::getSearchData($searchParam);

        // 返回分页数据
        return DictData::page($where, self::getPageData(),'*', ['sort' => 'desc', 'id' => 'desc'], ['status_desc']);
    }

    /**
     * 查看字典数据详情
     * @param $params
     * @return array
     */
    public static function detail(array $params) : array
    {
        $data = DictData::where(['id' => $params['id']])
            ->findOrEmpty();
        if ($data->isEmpty()) return [];

        return $data->toArray();
    }

    /**
     * 添加字典数据
     * @param array $params
     * @return bool
     */
    public static function add(array $params)
    {
        return DictData::add([
            'name' => $params['name'],
            'value' => $params['value'],
            'type_id' => $params['type_id'] ?? '',
            'remark' => $params['remark'] ?? '',
            'sort' => $params['sort'] ?? 0,
            'status' => $params['status'],
        ]);
    }


    /**
     * 编辑字典数据
     * @param array $params
     * @return bool
     */
    public static function edit(array $params) : bool
    {
        return DictData::edit([
            'name' => $params['name'],
            'value' => $params['value'],
            'type_id' => $params['type_id'] ?? '',
            'remark' => $params['remark'] ?? '',
            'sort' => $params['sort'] ?? 0,
            'status' => $params['status'],
        ], $params['id']);
    }


    /**
     * 删除字典数据
     * @param array $params
     * @return bool
     */
    public static function delete(array $params)
    {
        return DictData::del($params['id']);
    }
}